/*
 ============================================================================
 Name		: FbSoftKeyBar.cpp
 Author	  : ceze
 Version	 : 1.0
 Copyright   : FlashBox for ver.Zhongyou(���ʰ�)
 Description : CFbSoftKeyBar implementation
 ============================================================================
 */

#include "FbSoftKeyBar.h"

#include <coemain.h>
#include "ConstDefined.hrh"
#include "Flashbox4.hrh"
#include "Flashbox4.mbg"//mbm图头文件
#include "Flashbox4_0x20021B21.rsg"
#include <EIKENV.H>
#include <stringloader.h>
#include <libc\time.h>

CFbSoftKeyBar::CFbSoftKeyBar (MSoftKeyObserver *pObserver )
{
	// No implementation required
	m_pObserver = pObserver;
	m_leftKeyId = -1;
	m_rightKeyId = -1;
}

CFbSoftKeyBar::~CFbSoftKeyBar ()
{
	iPeriodic->Cancel();
	delete iPeriodic;
	delete m_CurrentTime;
	delete m_pSoftKeyBar;
	delete m_pSoftLeftText;
	delete m_pSoftRightText;
	delete m_CurInfoText;
//	delete m_CurShowInfoTextPart;
	delete m_CurPageBuf;
}

CFbSoftKeyBar* CFbSoftKeyBar::NewLC (const TRect &aRect, CCoeControl *pParent, MSoftKeyObserver *pObserver )
{
	CFbSoftKeyBar* self = new (ELeave) CFbSoftKeyBar (pObserver );
	CleanupStack::PushL (self );
	self->ConstructL (aRect, pParent );
	return self;
}

CFbSoftKeyBar* CFbSoftKeyBar::NewL (const TRect &aRect, CCoeControl *pParent, MSoftKeyObserver *pObserver )
{
	CFbSoftKeyBar* self = CFbSoftKeyBar::NewLC (aRect, pParent, pObserver );
	CleanupStack::Pop (); // self;
	return self;
}

void CFbSoftKeyBar::ConstructL (const TRect &aRect, CCoeControl *pParent )
{
	CreateWindowL (pParent );
	//加载softkey图
	m_pSoftKeyBar = (CEikonEnv::Static ())->CreateBitmapL (KFlashboxImagePath,
			EMbmFlashbox4Softkeybar );
	
	iPeriodic = CPeriodic::NewL(CActive::EPriorityIdle);
	
	m_CurrentTime = HBufC::NewL(10);
	iPeriodic->Start(0,KFb_ShowTimeInterval,TCallBack(CFbSoftKeyBar::DrawCallback,this));
	
	m_ECurShowType = EShowTime;
	//ShowTime();
	ShowInfomation(R_TIP_GETMORE);
	SetRect (aRect );
	
}

/**
 * From CoeControl,SizeChanged.
 */
void CFbSoftKeyBar::SizeChanged ()
{
	DrawNow ();
}

/*
 * From CoeControl,
 * 按键事件接收处理
 */
TKeyResponse CFbSoftKeyBar::OfferKeyEventL (const TKeyEvent &aKeyEvent, TEventCode/* aType */)
{
	TInt event = 0;
	switch (aKeyEvent.iCode )
	{
	case EKeySoftLeft://左软件切换选项菜单
		event = m_leftKeyId;
		break;
	case EKeySoftRight://右软件切换左tab
		event = m_rightKeyId;
		break;
	default:
		return EKeyWasNotConsumed;
	}

	if (m_pObserver )
	{
		m_pObserver->SoftKeyEvent (event );
	}
	return EKeyWasConsumed;
}

/**
 * From CCoeControl,Draw.
 */
void CFbSoftKeyBar::Draw (const TRect& aRect ) const
{
	
	CWindowGc& gc = SystemGc ();
	TRect drawRect (Rect () );
	gc.SetPenStyle (CGraphicsContext::ENullPen );
	gc.SetBrushColor (KFbBackgroundColor );
	gc.SetBrushStyle (CGraphicsContext::ESolidBrush );
	gc.Clear (drawRect );

	if (m_pSoftKeyBar )
	{
		gc.DrawBitmap (drawRect, m_pSoftKeyBar );
	}

	// create font
	CFont* font = NULL;
	// Create font specification.
	TFontSpec fontSpec = iCoeEnv->NormalFont ()->FontSpecInTwips ();
	//set font's size 80%
	fontSpec.iHeight = 8 * fontSpec.iHeight / 10;
	//apply font
	iCoeEnv->ScreenDevice ()->GetNearestFontInTwips (font, fontSpec );
	if (font == NULL )
		gc.UseFont (iCoeEnv->NormalFont () );
	else
		gc.UseFont (font );
	gc.SetPenColor (KRgbWhite);
	//left text
	if (m_pSoftLeftText )
	{
		gc.DrawText (*m_pSoftLeftText, TPoint (drawRect.iTl.iX + 5,
				drawRect.iBr.iY - 3) );
	}
	//right text
	if (m_pSoftRightText )
	{
		gc.DrawText (*m_pSoftRightText, TPoint (drawRect.iBr.iX
				- KFbSoftKeyTextSize.iWidth, drawRect.iBr.iY - 3 ) );
	}
	
	gc.DiscardFont ();
	iCoeEnv->ScreenDevice ()->ReleaseFont (font );
		
	fontSpec = iCoeEnv->NormalFont ()->FontSpecInTwips ();
	//set font's size 70%
	fontSpec.iHeight = 7 * fontSpec.iHeight / 10;
	//apply font
	iCoeEnv->ScreenDevice ()->GetNearestFontInTwips (font, fontSpec );
	if (font == NULL )
		gc.UseFont (iCoeEnv->NormalFont ());
	else
		gc.UseFont (font );
	gc.SetPenColor (KRgbGray);

	//Show Middle Content of SoftKey Bar!
	gc.SetBrushStyle (CGraphicsContext::ENullBrush );
	TRect ContentRect(drawRect.iTl.iX + KFbSoftKeyTextSize.iWidth + 15, drawRect.iTl.iY,
				drawRect.iBr.iX	- KFbSoftKeyTextSize.iWidth -15, drawRect.iBr.iY);
	
	switch(m_ECurShowType)
	{
	case EShowTime:
		if (m_CurrentTime)
		{
			gc.DrawText(*m_CurrentTime, ContentRect, ContentRect.iBr.iY - 5,
					CGraphicsContext::ECenter);
		}
		break;
	case EShowInfoText:
		if (m_CurInfoText)
		{
			TInt textWidth = font->TextWidthInPixels(m_CurInfoText->Des());
			static TInt textOffset = ContentRect.Width();
			if(textWidth < ContentRect.Width())
			{
				textOffset = 0;
			}
			else
			{
				textOffset -= 3;
				if(textOffset + textWidth < ContentRect.Width() / 2)
					textOffset = ContentRect.Width()/2;
			}
			gc.DrawText(*m_CurInfoText, ContentRect, ContentRect.iBr.iY
					- 3, CGraphicsContext::ECenter, textOffset);
		}
		break;
	case EShowPageNums:
		if (m_CurPageBuf)
		{
			gc.DrawText(*m_CurPageBuf, ContentRect, ContentRect.iBr.iY - 3,
					CGraphicsContext::ECenter);
		}
		break;
	default:
		break;
	}
	//Show current local time
	gc.DiscardFont ();
	iCoeEnv->ScreenDevice ()->ReleaseFont (font );
	
	if(IsDimmed())
	{
		gc.SetBrushColor(KFbOptionBackColorAlpha);
		gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
		gc.DrawRect(drawRect);
	}
	
}

/**
 * Left软键文字&id
 * 如果禁用左键功能可将参数是指为：aKeyId = 0， aResId = -1
 */
void CFbSoftKeyBar::SetLeftKeyL (const TInt &aKeyId, const TInt &aResId )
{
	m_leftKeyId = aKeyId;
	if (m_pSoftLeftText )
	{
		delete m_pSoftLeftText;
		m_pSoftLeftText = NULL;
	}
	if(aResId != -1)//Disable LeftSoftkey!
		m_pSoftLeftText = StringLoader::LoadL (aResId );
}

/**
 * Right Key
 *  如果禁用右键功能可将参数是指为：aKeyId = 0， aResId = -1
 */
void CFbSoftKeyBar::SetRightKeyL (const TInt &aKeyId, const TInt &aResId )
{
	m_rightKeyId = aKeyId;
	if (m_pSoftRightText )
	{
		delete m_pSoftRightText;
		m_pSoftRightText = NULL;
	}
	if(aResId != -1)//Disable RightSoftkey!
		m_pSoftRightText = StringLoader::LoadL (aResId );
}

TInt CFbSoftKeyBar::DrawCallback(TAny* pInst)
{
	CFbSoftKeyBar* pThis = (CFbSoftKeyBar*)pInst;	
	if(pThis->IsDimmed())
		return KErrNone;
	
	switch(pThis->m_ECurShowType)
	{
	case EShowTime:
		pThis->ShowTime();
		break;
	case EShowInfoText:
		if(pThis->m_CurInfoText)
		{
			pThis->DrawNow();
		}
		break;
	default:
		break;
	}
	
	//return;
}

void CFbSoftKeyBar::ShowTime()
{
	if(m_ECurShowType != EShowTime)
	{
		m_ECurShowType = EShowTime;
		iPeriodic->Cancel();
		iPeriodic->Start(0,KFb_ShowTimeInterval,TCallBack(CFbSoftKeyBar::DrawCallback,this));
	}
	
	TTime currentTime;
	currentTime.HomeTime();

	TBuf<32> timeAsText;
	_LIT (KTimeFormat, "%H : %T");
	TRAPD (err, currentTime.FormatL(timeAsText, KTimeFormat););
	
	m_CurrentTime->Des().Copy(timeAsText);

	DrawNow();
}

void CFbSoftKeyBar::ShowInfomation(const TDesC &aInfoText)
{
	if(m_ECurShowType == EShowInfoText)
		return;
	
	iPeriodic->Cancel();
	if (m_CurInfoText)
	{
		delete m_CurInfoText;
	}
	m_CurInfoText = aInfoText.AllocL();
	m_ECurShowType = EShowInfoText;
	m_textOffset = 0;
	iPeriodic->Start(10000, KFb_ShowInfoInterval,TCallBack(CFbSoftKeyBar::DrawCallback, this));
}

void CFbSoftKeyBar::ShowInfomation(const TInt aTextId)
{
	if(m_ECurShowType == EShowInfoText)
		return;
	
	iPeriodic->Cancel ();
	if (m_CurInfoText )
	{
		delete m_CurInfoText;
	}
	m_CurInfoText = StringLoader::LoadL (aTextId );
	m_ECurShowType = EShowInfoText;
	m_textOffset = 0;
	iPeriodic->Start (10000, KFb_ShowInfoInterval, TCallBack (
			CFbSoftKeyBar::DrawCallback, this ) );
	
}

void CFbSoftKeyBar::ShowPageNumbers(TInt aCurPageIndex, TInt aTotlePages)
{
	if(m_ECurShowType != EShowPageNums)
	{
		m_ECurShowType = EShowPageNums;
		iPeriodic->Cancel();		
	}
	
	if(m_CurPageBuf)
	{
		delete m_CurPageBuf;
	}
	TBuf<10> tmpPageBuf;
	tmpPageBuf.Format(_L("%d / %d"), aCurPageIndex, aTotlePages);
	m_CurPageBuf = tmpPageBuf.AllocL();
	
	DrawNow();
}
